package com.cxg.fairy.ec.main.index.ad;

import android.graphics.Paint;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.ec.detail.IGoodsDetailDelegate;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.cxg.fairy.ui.widget.CountDownTimer.MainDownTimerView;
import com.cxg.fairy.ui.widget.CountDownTimer.OnCountDownTimerListener;
import com.diabin.latte.ec.R;

import java.util.List;

/**
 * Created by CFairy
 */

public final class RushBuyListAdapter extends MultipleRecyclerAdapter {

    private boolean mIsRushItem = false;

    private RushBuyListDelegate mDelegate = null;

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    RushBuyListAdapter(List<MultipleItemEntity> data,RushBuyListDelegate delegate) {
        super(data);
        //添加购物测item布局
        addItemType(RushBuyListItemType.RUSH_LIST_VERTICAL, R.layout.item_rush_buy_list);
        addItemType(RushBuyListItemType.RUSH_BUY_LIST_HEADER,R.layout.item_rush_buy);
        mDelegate = delegate;
    }

    @Override
    protected void convert(MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);

        //先取出所有值
        final int skuId = entity.getField(MultipleFields.ID);
        final String rushBuyStatus = entity.getField(ADGoodsListItemFields.RUSH_BUY_STATUS);
        final int rushBuyTimeLeft = entity.getField(ADGoodsListItemFields.RUSH_BUY_TIME_LEFT);
        final String adGoodsName = entity.getField(ADGoodsListItemFields.AD_GOODS_NAME);
        final String defaultPrice = entity.getField(ADGoodsListItemFields.DEFAULT_PRICE);
        final String unit = entity.getField(ADGoodsListItemFields.UNIT);
        final String thumb = entity.getField(ADGoodsListItemFields.THUMB);
        final String price = entity.getField(ADGoodsListItemFields.PRICE);

        //所有控件

        final AppCompatTextView tvRushBuyStatus = holder.getView(R.id.tv_rush_buy_status);

        final AppCompatImageView tvADGoodsThumb = holder.getView(R.id.tv_ad_goods_img_a);
        final AppCompatTextView tvADGoodsName = holder.getView(R.id.tv_ad_goods_name_a);
        final AppCompatTextView tvADGoodsDefaultPrice = holder.getView(R.id.tv_ad_goods_default_price_a);
        final AppCompatTextView tvADGoodsUnit = holder.getView(R.id.tv_ad_goods_unit_a);
        final AppCompatTextView tvADGoodsPrice = holder.getView(R.id.tv_ad_goods_price_a);

        final AppCompatTextView tvRushBuyBtn = holder.getView(R.id.tv_rush_buy_item_detail);


        MainDownTimerView mMainDownTimerView;

        switch (holder.getItemViewType()) {

            case RushBuyListItemType.RUSH_BUY_LIST_HEADER:
                if (!mIsRushItem) {
                    tvRushBuyStatus.setText(rushBuyStatus);

                    // 1 . initViews
                    mMainDownTimerView = holder.getView(R.id.mdtv_left_time);
                    mMainDownTimerView.setDownTime(rushBuyTimeLeft);
                    // 2 . initEvents
                    mMainDownTimerView.setDownTimerListener(new OnCountDownTimerListener() {

                        @Override
                        public void onFinish() {
                            Toast.makeText(mContext, "抢购结束", Toast.LENGTH_SHORT).show();
                        }
                    });
                    // 3 . StartDownTimer
                    mMainDownTimerView.startDownTimer();
                    mIsRushItem = true;
                }
                break;
            case RushBuyListItemType.RUSH_LIST_VERTICAL:
                Glide.with(mContext)
                        .load(thumb)
                        .apply(OPTIONS)
                        .into(tvADGoodsThumb);
                tvADGoodsName.setText(adGoodsName);
                tvADGoodsDefaultPrice.setText(defaultPrice);
                tvADGoodsDefaultPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                tvADGoodsUnit.setText("/"+unit);
                tvADGoodsUnit.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                tvADGoodsPrice.setText(price);

                tvRushBuyBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mDelegate.getSupportDelegate().start(IGoodsDetailDelegate.create(skuId));
                    }
                });

                break;
            default:
                break;
        }

    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getField(ADGoodsListItemFields.SPAN_SIZE);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}
